local zhuanshu = fk.CreateSkill{
  name = "xiaobai__zhuanshu",
}

Fk:loadTranslationTable{
  ["xiaobai__zhuanshu"] = "撰疏",
  [":xiaobai__zhuanshu"] = "你可以摸两张牌或弃置两张牌并展示之以视为使用一张基本牌，"..
    "然后若这三者牌名均不同，则此技能失效至你执行另一项。",
  
  ["@xiaobai__zhuanshu"] = "撰疏",
  ["xiaobai__zhuanshu_draw"] = "须摸牌",
  ["xiaobai__zhuanshu_discard"] = "须弃牌",
}

zhuanshu:addEffect("viewas", {
  anim_type = "special",
  pattern = ".|.|.|.|.|basic",
  interaction = function (self, player)
    local cards = Fk:getAllCardNames("b", false, false)
    local view_as_cards = player:getViewAsCardNames(zhuanshu.name, cards)
    return UI.CardNameBox{
      choices = view_as_cards,
    }
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return #selected < 2
  end,
  view_as = function (self, player, cards)
    if #cards ~= 0 and #cards ~= 2 then return end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = zhuanshu.name
    if #cards == 2 then
      c:addFakeSubcards(cards)
    end
    return c
  end,
  before_use = function (self, player, use)
    local room = player.room
    local cards = {}
    local discard = false
    if not use.card.fake_subcards or #use.card.fake_subcards == 0 then
      cards = player:drawCards(2, zhuanshu.name)
      player:showCards(cards)
    else
      cards = use.card.fake_subcards
      room:throwCard(cards, zhuanshu.name, player)
      room:showCards(cards)
      discard = true
    end
    if Fk:getCardById(cards[1]).trueName ~= Fk:getCardById(cards[2]).trueName 
      and Fk:getCardById(cards[1]).trueName ~= use.card.trueName
      and Fk:getCardById(cards[2]).trueName ~= use.card.trueName then
      room:invalidateSkill(player, "xiaobai__zhuanshu")
      room:setPlayerMark(player, "@xiaobai__zhuanshu", 
        discard and "xiaobai__zhuanshu_draw" or "xiaobai__zhuanshu_discard")
    end
  end,
  enabled_at_play = Util.TrueFunc,
  enabled_at_response = function (self, player, response)
    return not response
  end 
})

zhuanshu:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(zhuanshu.name, true, true) and (player:getMark("@xiaobai__zhuanshu") == "xiaobai__zhuanshu_draw" 
      or player:getMark("@xiaobai__zhuanshu") == "xiaobai__zhuanshu_discard")
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@xiaobai__zhuanshu")
    local discard_num = 0
    for _, move in ipairs(data) do
      if move.toArea ==  Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.from == player then
        discard_num = discard_num + #move.moveInfo
      end      
    end
    local draw_num = 0
    for _, move in ipairs(data) do
      if move.toArea ==  Player.Hand and move.moveReason == fk.ReasonDraw and move.to == player then
        draw_num = draw_num + #move.moveInfo
      end      
    end
    if mark == "xiaobai__zhuanshu_draw" and draw_num == 2 then
      room:setPlayerMark(player, "@xiaobai__zhuanshu", 0)
      room:validateSkill(player, "xiaobai__zhuanshu")
    end
    if mark == "xiaobai__zhuanshu_discard" and discard_num == 2 then
      room:setPlayerMark(player, "@xiaobai__zhuanshu", 0)
      room:validateSkill(player, "xiaobai__zhuanshu")
    end
  end
})
return zhuanshu